package com.crs.task;

import com.crs.entity.Borrow_record;
import com.crs.mapper.BorrowMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class CheckBorrowRecordsEveryMin {

    @Autowired
    private BorrowMapper borrowMapper;

    /**
     * 每分钟处理一次，看是否准备要归还，还是预期
     */
    @Scheduled(cron = "0 * * * * ? ")//每分钟处理一次
    public void checkBorrowRecords(){
        //获取快要过期的记录
        List<Borrow_record> records = borrowMapper.findOverTimeAndStatus();

        //提醒那些快到期的人还东西
        for (Borrow_record br:records
             ) {
            //TODO 给用户发送请求，让他们准备还物品
            //发完请求修改用户字段，每天只发一次
            br.setHasReminded(1);
            borrowMapper.updateOverTimeAndStatus(br);
        }

        //获取已经逾期的记录
        List<Borrow_record> hasOverTimeRecords = borrowMapper.findHasOverTime();

        //修改记录状态为已逾期，然后在每天的定时任务中设置罚款金额
        for (Borrow_record by:hasOverTimeRecords
             ) {
            borrowMapper.updateHasOverTime(by);
        }
    }
}
